package com.xyw.code.springcloudsecurityoath2.constant;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 自定义401错误码内容
 * ========================
 * Created with IntelliJ IDEA.
 * User：恒宇少年
 * Date：2017/4/22
 * Time：16:53
 * 码云：http://git.oschina.net/jnyqy
 * ========================
 */
@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {
        private final Logger log = LoggerFactory.getLogger(CustomAuthenticationEntryPoint.class);

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.error("Spring Securtiy异常", authException);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print("Access Denied");
    }
//    private final Logger log = LoggerFactory.getLogger(CustomAuthenticationEntryPoint.class);
//
//    @Override
//    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
//
//        log.info("Pre-authenticated entry point called. Rejecting access");
//        httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Access Denied");
//    }
}